package com.cloudbroker.bcs.platform.ltb.common.payUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.interfaces.RSAPublicKey;

public class GetPublicKeyJFrame extends JFrame {
	/**
		 * 
		 */
	private static final long serialVersionUID = 1L;
	GridBagLayout g = new GridBagLayout ();
	GridBagConstraints c = new GridBagConstraints ();

	GetPublicKeyJFrame (String str) {

		super (str);
		setSize (400, 400);
		setBackground (new Color (255, 255, 128));
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setLayout (g);
		// 调用方法
		addComponent ();
		setVisible (true);
		setLocationRelativeTo (null);// 设居中显示;
	}

	// 在这个方法中将会添加所有的组件;
	// 使用的网格包布局;希望楼主能看懂;
	public void addComponent() {

		submit = new JButton ("获取公钥");
		submit.addActionListener (new ActionListener () {

			@Override
			public void actionPerformed(ActionEvent e) {

				jta2.setText ("");
				String encrypt = jta.getText ();
				String[] encryptArray = encrypt.split ("\n");
				StringBuilder sb = new StringBuilder ();
				for (String encryptStr : encryptArray) {
					if (!encryptStr.isEmpty ()) {
						try {
							RSAPublicKey publicKey = RSAUtils.getPublicKey (encryptStr);
							sb.append (Base64Utils.encode (publicKey.getEncoded ())).append ("\n");
						}
						catch (Exception e1) {
							System.out.println ("DES加密出错");
						}
					}
					if (encryptArray.length > 1) {
						sb.append ("\r\n");
					}
				}
				jta2.setText (sb.toString ());

			}
		});

		jta = new JTextArea (10, 15);
		JScrollPane scroll = new JScrollPane (jta);
		jta2 = new JTextArea (10, 15);
		JScrollPane scroll2 = new JScrollPane (jta2);
		jlb = new JLabel ("证书绝对路径（.cer）");
		jlb2 = new JLabel ("公钥");
		jta.setLineWrap (true);
		jta.setAutoscrolls (true);
		jta.setMinimumSize (new Dimension (20, 20));
		jta2.setAutoscrolls (true);
		jta2.setLineWrap (true);
		add (g, c, jlb, 2, 0, 1, 1);
		add (g, c, scroll, 2, 1, 1, 1);
		add (g, c, jlb2, 3, 0, 1, 1);
		add (g, c, scroll2, 3, 1, 1, 1);
		add (g, c, submit, 2, 3, 1, 1);
		// add(g,c,submit2,3,3,1,1);
		// add(g,c,submit3,2,4,1,1);
		// add(g,c,submit4,3,4,1,1);
	}

	public void add(GridBagLayout g, GridBagConstraints c, JComponent jc, int x, int y, int gw, int gh) {
		c.gridx = x;
		c.gridy = y;
		c.anchor = GridBagConstraints.CENTER;
		c.gridwidth = gw;
		c.gridheight = gh;
		c.weightx = 5;
		c.weighty = 5;
		g.setConstraints (jc, c);
		add (jc);
	}

	public static void main(String args[]) {
		new GetPublicKeyJFrame ("公钥获取工具");
	}

	JButton submit;
	// JButton submit2;
	JButton submit3;
	JButton submit4;
	JLabel jlb;
	JLabel jlb2;
	JTextArea jta;
	JTextArea jta2;

}